package cn.louzen.utils;

import cn.louzen.domain.SampleOrg;

import java.util.Properties;

public class SampleOrgUtil {

    public static SampleOrg getSampleOrg() throws Exception {
        // 读取配置文件，以这些配置文件内容组装一个SampleOrg对象
        Properties prop = Util.getProperties();

        // 设置SampleOrg对象的name和mspid属性，并生成SampleOrg对象
        SampleOrg sampleOrg = new SampleOrg(StringUtil.realStr(prop.getProperty("SampleOrg.name")),
                StringUtil.realStr(prop.getProperty("SampleOrg.mspid")));

        // 设置domainName
        sampleOrg.setDomainName(StringUtil.realStr(prop.getProperty("SampleOrg.domainName")));

        // 设置caName、caLocation、caProperties、caClient
        sampleOrg.setCAName(StringUtil.realStr(prop.getProperty("SampleOrg.ca.caName")));
        sampleOrg.setCALocation(StringUtil.realStr(prop.getProperty("SampleOrg.ca.caLocation")));
        sampleOrg.setCAProperties(null);
        // 给sampleOrg添加一个已经设置好加密套件的CAClient对象
        CAUtil.getCAClient(sampleOrg);

        // 设置peerAdmin
        sampleOrg.setPeerAdmin(SampleUserUtil.getSampleUser("peerAdmin", sampleOrg));

        // 设置admin,admin需要在userMap前面
        sampleOrg.setAdmin(SampleUserUtil.getSampleUser("admin", sampleOrg));

        // 设置userMap，里面包含userx，需要在设置admin后面
        sampleOrg.addUser(SampleUserUtil.getSampleUser("user0", sampleOrg));

        // 读取并存入peerLocations、ordererLocations、eventHubLocations
        sampleOrg.setPeerLocations(StringUtil.stringToMap(prop.getProperty("SampleOrg.locations.peerLocations"),
                ",", "@"));
        sampleOrg.setOrdererLocations(StringUtil.stringToMap(prop.getProperty("SampleOrg.locations.ordererLocations"),
                ",", "@"));
        sampleOrg.setEventHubLocations(StringUtil.stringToMap(prop.getProperty("SampleOrg.locations.eventHubLocations"),
                ",", "@"));

        return sampleOrg;
    }

}
